package com.uphone.kingmall.activity.order;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.uphone.kingmall.adapter.RvOrderRefundAdapter;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.bean.OrderBean;
import com.uphone.kingmall.listener.NetListCallBack;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.MyUrl;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * <pre>
 *     author : 孙增艳
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2019/2/28
 *     desc   : 退款退货列表页面
 *     version: 1.0
 * </pre>
 */
public class OrderRefundListActivity extends BaseActivity {

    @BindView(R.id.rv_order_refund)
    RecyclerView rvOrderRefund;
    @BindView(R.id.srl_order_refund)
    SmartRefreshLayout srlOrderRefund;
    SmartRefreshLayout srlWait;
    @BindView(R.id.tv_empty)
    TextView tvEmpty;
    int page = 1;

    private List<OrderBean.DataBean> orderRefundBeanList = new ArrayList<>();
    private RvOrderRefundAdapter refundAdapter;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_order_refund;
    }

    @Override
    public void initView() {
        tvEmpty.setText("暂无待退款订单");
        setStatusBar();

        //设置是否打开自动加载更多
        srlOrderRefund.setEnableAutoLoadMore(false);
        //取消内容不满一页时开启上拉加载功能
        srlOrderRefund.setEnableLoadMoreWhenContentNotFull(false);

        srlOrderRefund.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                loadData(1);
            }
        });
        srlOrderRefund.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                loadData(++page);
            }
        });
        //代码设置刷新加载样式
        srlOrderRefund.setPrimaryColorsId(R.color.colorBg, R.color.black);//全局设置主题颜色
        srlOrderRefund.setRefreshHeader(new ClassicsHeader(this));
        srlOrderRefund.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));

        refundAdapter = new RvOrderRefundAdapter(this);
        rvOrderRefund.setLayoutManager(new LinearLayoutManager(this));
        rvOrderRefund.setAdapter(refundAdapter);
        refundAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_check_details:
                        CommonUtil.startIntent(OrderRefundListActivity.this, OrderRefundDetailsActivity.class, orderRefundBeanList.get(position).getOrderId());
                        break;
                    default:
                        break;
                }
            }
        });

    }

    @Override
    public void initData() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData(1);
    }

    @Override
    public void loadJPush() {
        super.loadJPush();
        loadData(1);
    }

    private void loadData(int page) {
        this.page = page;
        HttpParams params = new HttpParams();
        params.put("page", page);
        params.put("limit", "10");
        params.put("orderState", "60");
        CommonUtil.netRv(new OrderBean(), refundAdapter, orderRefundBeanList, MyUrl.getUserOrders, params, page, new NetListCallBack<OrderBean.DataBean>() {
            @Override
            public void callBack(List<OrderBean.DataBean> list) {
                orderRefundBeanList = list;

                if (orderRefundBeanList == null || orderRefundBeanList.size() == 0) {
                    tvEmpty.setVisibility(View.VISIBLE);
                } else {
                    tvEmpty.setVisibility(View.GONE);
                }
            }
        });
    }
}
